﻿@using Jb.Framework.Common.Forms.Validation
@model Jb.Framework.Common.Forms.JbDatePicker

@{

    var titlevalue = Model.Title;
    // if field is required add star
    if (Model.Validations.Any(t => t.Type == ElementValidationType.Required))
    {
        titlevalue += " <span class='star'>*</span>";
    }
    var labelHtmlAttr = new Dictionary<string, object> { { "class", "Custom-label" }, { "data-value", titlevalue } };


    var textBoxHtmlAttr = new Dictionary<string, object> { { "class", "input-medium text-center CF-datepicker" } };
    textBoxHtmlAttr.Add("placeholder", Model.HelpText);

    if (Model.Validations.Any(x => x.Type != ElementValidationType.None))
    {
        textBoxHtmlAttr.Add("data-val", "true");
    }

    if (Model.Validations.Any(x => x.Type == ElementValidationType.Required))
    {
        textBoxHtmlAttr.Add("data-Val-Required", Model.Validations.First(x => x.Type == ElementValidationType.Required).Message);
    }

    if (Model.Validations.Any(x => x.Type == ElementValidationType.Range))
    {
        var rangevalidation = Model.Validations.Single(x => x.Type == ElementValidationType.Range) as ElementDatePickerRangeValidation;
        textBoxHtmlAttr.Add("data-val-range-min", rangevalidation.MinValue);
        textBoxHtmlAttr.Add("data-val-range-max", rangevalidation.MaxValue);
        textBoxHtmlAttr.Add("data-val-range", Model.Validations.First(x => x.Type == ElementValidationType.Range).Message);
        //textBoxHtmlAttr.Add("data-val-number", "Value Must Be Number");
    }
}

<div class="pull-left form-group">
    @Html.Hidden("ModelType", Model.GetType())

    @for (var i = 0; i < Model.Validations.Count; i++)
    {
        var hiddnModelBindername = "Validations[" + i + "].QVModelType"; 
        @Html.Hidden(hiddnModelBindername, Model.Validations[i].GetType())
       
        @Html.HiddenFor(x => x.Validations[i].IsChecked)
        @Html.HiddenFor(x => x.Validations[i].Type)
        @Html.HiddenFor(x => x.Validations[i].Message)

        if (Model.Validations[i] is ElementRangeValidation)
        {

            // these properties is in derived class and for correct binding we nedd this
            var validations = Model.Validations[i] as ElementRangeValidation;
            var hiddenMinModelName = "Validations[" + i + "].MinValue";
            var hiddenMaxModelName = "Validations[" + i + "].MaxValue";
        @Html.Hidden(hiddenMinModelName, validations.MinValue)
        @Html.Hidden(hiddenMaxModelName, validations.MaxValue)
        }


    }

    @Html.LabelFor(x => x.Value, htmlAttributes: labelHtmlAttr)
    @Html.HiddenFor(x => x.Title)
    @Html.HiddenFor(x => x.ElementId)
    @Html.HiddenFor(x => x.HelpText)
    @foreach (var accRole in Model.AccessRole)
    { 
        @Html.HiddenFor(modelItem => accRole)
    }

    @Html.TextBoxFor(x => x.Value, "{0:MM/dd/yyyy }", htmlAttributes: textBoxHtmlAttr)
    @Html.ValidationMessageFor(model => model.Value)

</div>

<script type="text/javascript">
    $(".CF-datepicker").datepicker();


    //get data-value and set to html to correct show value
    $(".Custom-label").each(function () {
        $(this).html($(this).data("value"));
    });

</script>
